package com.shuoyue.ycgk.ui.mine.order;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.entity.Order;
import com.shuoyue.ycgk.entity.OrderGoods;
import com.shuoyue.ycgk.entity.address.OrderAddressBean;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.utils.XToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderInfoActivity extends BaseMvpActivity<OrderContract.Presenter>
        implements OrderContract.View {


    @BindView(R.id.back)
    ImageButton back;
    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.l_right)
    FrameLayout lRight;
    @BindView(R.id.tv_addrName)
    TextView tvAddrName;
    @BindView(R.id.tv_addrDetail)
    TextView tvAddrDetail;
    @BindView(R.id.address_layout)
    LinearLayout addressLayout;
    @BindView(R.id.logo)
    ImageView logo;
    @BindView(R.id.statue)
    TextView statue;
    @BindView(R.id.goodsRecycleView)
    RecyclerView goodsRecycleView;
    @BindView(R.id.goodsTotalPrice)
    TextView goodsTotalPrice;
    @BindView(R.id.postPrice)
    TextView postPrice;
    @BindView(R.id.couponPrice)
    TextView couponPrice;
    @BindView(R.id.payActure)
    TextView payActure;
    @BindView(R.id.orderNum)
    TextView orderNum;
    @BindView(R.id.payTime)
    TextView payTime;
    @BindView(R.id.payType)
    TextView payType;
    @BindView(R.id.postNum)
    TextView postNum;
//    @BindView(R.id.copyPostNum)
//    ImageView copyPostNum;
    @BindView(R.id.lay_post_info)
    CardView layPostInfo;
    @BindView(R.id.service)
    TextView service;
    @BindView(R.id.lay_service)
    LinearLayout layService;

    Order order;
    OrderGoodsAdapter orderGoodsAdapter;

    public static void start(Context context, int id) {
        context.startActivity(new Intent(context, OrderInfoActivity.class)
                .putExtra("id", id)
        );
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_info;
    }

    @Override
    protected void initView() {
        pageTitle.setText("订单详情");
        goodsRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
        orderGoodsAdapter = new OrderGoodsAdapter(null);
        goodsRecycleView.setAdapter(orderGoodsAdapter);

    }

    @Override
    public void initData() {
        super.initData();
//        order = (Order) getIntent().getSerializableExtra("order");
//        setOrderInfo();
        mPresenter = new OrderContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getOrderInfo(getIntent().getIntExtra("id", -1));
    }


    @OnClick({R.id.back, R.id.copyPostNum, R.id.service})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.copyPostNum:
                getClip(order.getNo() + "");
                break;
            case R.id.service:
//                CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");
                if(Constant.isOpenServiceExploer){
                    Uri uri = Uri.parse(Constant.serviceUrl);
                    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent);
                }else {
                    CstWebActivity.start(mContext, Constant.serviceUrl, "咨询");

                }

                break;
        }
    }

    private void getClip(String text) {
        //获取剪贴板管理器
        ClipboardManager cm = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
// 创建普通字符型ClipData
        ClipData mClipData = ClipData.newPlainText("Label", text);
// 将ClipData内容放到系统剪贴板里。
        cm.setPrimaryClip(mClipData);
        XToast.toast(mContext, "已复制到粘贴板");
    }


    @Override
    public void setOrderList(List<Order> orderInfo) {
        this.order = orderInfo.get(0);
        List<OrderGoods> orderGoodsList = new ArrayList<>();
        OrderGoods orderG = new OrderGoods();
        orderG.setProductId(order.getProductId());
        orderG.setProductNum(order.getProductNum());
        orderG.setProductPrice(order.getProductPrice());
        orderG.setProductType(order.getProductType());
        orderG.setTitle(order.getTitle());

        orderGoodsList.add(orderG);

        orderGoodsAdapter.setNewData(orderGoodsList);

        //价格信息
        goodsTotalPrice.setText("￥" + order.getCostPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
        postPrice.setText("￥" + order.getPostagePrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
        couponPrice.setText("￥" + order.getDiscountAmount());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
        payActure.setText("￥" + order.getPayPrice());//((item.getPrice() % 1f == 0) ? ((int) item.getPrice()) : item.getPrice())
        orderNum.setText(order.getProductNum() + "");
        payTime.setText(order.getPayTime());
        payType.setText(order.getPayType() + "");

        //地址信息
        OrderAddressBean addressBean = order.getDeliveryAddress();
        if (addressBean == null) {
            addressLayout.setVisibility(View.GONE);
            layPostInfo.setVisibility(View.GONE);
        } else {
            tvAddrName.setText(addressBean.getContact() + "\t" + addressBean.getPhone());
            tvAddrDetail.setText(addressBean.getProvinceName()
                    + addressBean.getCityName()
                    + addressBean.getRegionName()
                    + addressBean.getAddress());
            postNum.setText(order.getNo());
        }
        //状态
        String statueStr = "";
        if (order.getProductType() == 2
                && order.getIsReceipt() == 1) {
            statueStr = "已发货";
        } else if (order.getProductType() == 2
                && order.getIsReceipt() == 0) {
            statueStr = "待发货";
        } else {
            statueStr = "已完成";
        }
        statue.setText(statueStr);

        //订单信息
        orderNum.setText(order.getOrderNo());
        payTime.setText(order.getPayTime());

        payType.setText(order.getPayType() == 1 ? "微信" : "支付宝");


    }
}
